package com.szh.cloud.file.control;

import com.alibaba.fastjson.JSON;
import com.szh.cloud.file.entity.SearFileEntity;
import com.szh.cloud.file.service.FileService;
import com.szh.cloud.file.service.FileSynService;
import com.szh.cloud.mybatis.entity.PtFileEntity;
import com.szh.cloud.mybatis.flexigrid.FlexResEntity;
import com.szh.cloud.mybatis.flexigrid.PagingBean;
import com.szh.cloud.spring.control.BaseController;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import java.io.IOException;

@Controller
@RequestMapping("/file")
public class FileController extends BaseController {
    private static Log logger = LogFactory.getLog(FileController.class);
    @Resource
    private FileService fileService;
    @Resource
    private FileSynService fileSynService;
    @RequestMapping("/fileAll")
    public String fileAll(){
        return  "file/filePage";
    }


    @RequestMapping("/queryFileListByPage")
    @ResponseBody
    public FlexResEntity<PtFileEntity> queryFileListByPage(SearFileEntity entity, PagingBean paging){
        FlexResEntity<PtFileEntity> flexResEntity=fileService.queryFileListByPage(entity,paging);
        return flexResEntity;
    }

    @RequestMapping("/synchFileData")
    @ResponseBody
    public String synchFileData() {
        fileService.synchFileData();
        return successJSON("file.synch.data.success");
    }
    @RequestMapping("/queryParentFileById")
    @ResponseBody
    public String queryParentFileById(@RequestParam("fileUuid") String fileUuid){
        PtFileEntity entity = fileService.queryParentFileById(fileUuid);
        return JSON.toJSONString(entity);
    }

    @RequestMapping("/mkdir")
    @ResponseBody
    public String mkdir(@RequestParam("folderName") String folderName,@RequestParam("parentName") String parentName){
        fileService.mkdir(folderName,parentName);
        return successJSON("file.mkdir.success");
    }
    @RequestMapping("/uploadFile")
    @ResponseBody
    public String uploadFile(MultipartHttpServletRequest request){
        MultipartFile multipartFile = request.getFile("fileName");
        String fileName = multipartFile.getOriginalFilename();
        String parentPath = request.getParameter("parentPath");

        logger.info("fileName: " + fileName + "parentPath:" + parentPath);
        try {
            fileService.uploadFile(fileName,parentPath,multipartFile.getInputStream());
        } catch (IOException e) {
            //e.printStackTrace();
            return failJSON("file.upload.fail");
        }
        return successJSON("file.upload.success");
    }
    @RequestMapping("/downloadFile/{fileUuid}")
    @ResponseBody
    public ResponseEntity<byte[]> downloadFile(@PathVariable("fileUuid") String fileUuid) throws IOException {
        ResponseEntity<byte[]> responseEntity = fileService.downloadFile(fileUuid);
        return responseEntity;
    }

    @RequestMapping("/queryShareUrl")
    @ResponseBody
    public String queryShareUrl(@RequestParam("fileUuid") String fileUuid,@RequestParam("shareDate") Integer shareDate){
        String shareUrl = fileService.queryShareUrl(fileUuid,shareDate);
        return successCodeJSON("file.share.success",shareUrl);
    }
    @RequestMapping("/queryFileUrl")
    @ResponseBody
    public String queryFileUrl(@RequestParam("fileUuid") String fileUuid){
        String ShareUrl = fileService.queryFileUrl(fileUuid);
        return successCodeJSON("file.share.success",ShareUrl);
    }
    @RequestMapping("/copyPasteFile")
    @ResponseBody
    public String copyPasteFile(@RequestParam("fileUuidArray") String fileUuidArray,@RequestParam("descDir") String descDir){
        fileSynService.copyPasteFile(fileUuidArray,descDir);
        return successJSON("file.copy.success");
    }

}
